// 微信小程序兼容性修复脚本
const fs = require('fs');
const path = require('path');

// 需要修复的文件模式
const patterns = [
  { 
    regex: /gap:\s*(\d+)rpx;/g, 
    replacement: (match, p1) => {
      return `margin-right: ${p1}rpx;`
    }
  },
  {
    regex: /min-height:\s*(\d+)vh;/g,
    replacement: (match, p1) => `min-height: ${Math.round(p1 * 13.34)}rpx;`
  },
  {
    regex: /max-height:\s*(\d+)vh;/g,
    replacement: (match, p1) => `max-height: ${Math.round(p1 * 13.34)}rpx;`
  },
  {
    regex: /height:\s*(\d+)vh;/g,
    replacement: (match, p1) => `height: ${Math.round(p1 * 13.34)}rpx;`
  }
];

// 扫描并修复文件
function fixWeappCompatibility(dir) {
  const files = fs.readdirSync(dir);
  
  files.forEach(file => {
    const filePath = path.join(dir, file);
    const stat = fs.statSync(filePath);
    
    if (stat.isDirectory() && !file.startsWith('.') && file !== 'node_modules') {
      fixWeappCompatibility(filePath);
    } else if (file.endsWith('.vue') || file.endsWith('.scss') || file.endsWith('.css')) {
      let content = fs.readFileSync(filePath, 'utf8');
      let modified = false;
      
      patterns.forEach(pattern => {
        if (pattern.regex.test(content)) {
          content = content.replace(pattern.regex, pattern.replacement);
          modified = true;
        }
      });
      
      if (modified) {
        fs.writeFileSync(filePath, content);
        console.log(`已修复: ${filePath}`);
      }
    }
  });
}

console.log('开始修复微信小程序兼容性问题...');
fixWeappCompatibility('./src');
console.log('修复完成！');